using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Data;
using System.Runtime.InteropServices;
using PSLib = Microsoft.Office.Project.Server.Library;

namespace Microsoft.ProjectServer.QueueWatch
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ProgId("QueueWatch.Queue")]
    public class Queue 
    {
        private static wsLogin.LoginWindows login = new wsLogin.LoginWindows();
        private static wsFormsLogin.LoginForms loginForms = new wsFormsLogin.LoginForms();
        private static wsQueueSystem.QueueSystem queue = new wsQueueSystem.QueueSystem();

        private static wsQueueSystem.QueueStatusDataSet dsQueue;
        private static CookieContainer cookies = new CookieContainer();
        private static DataView dv = new DataView();

        public Queue()
        {
        }

        public void Login(string PSURL)
        {
            string URL = PSURL;
            if (!URL.EndsWith("/")) URL = URL + "/";

            login.Url = URL + "_vti_bin/PSI/loginWindows.asmx";
            login.Credentials = CredentialCache.DefaultCredentials;
            queue.Url = URL + "_vti_bin/PSI/queuesystem.asmx";
            queue.Credentials = CredentialCache.DefaultCredentials;

            if (!login.Login())
            {
                throw new Exception("Could not log into Project Server at " + URL);
            }
        }

        public void Logoff()
        {
            login.Logoff();
        }

        public void Login(string PSURL, string user, string password)
        {
            string URL = PSURL;
            if (!URL.EndsWith("/")) URL = URL + "/";

            loginForms.Url = URL + "_vti_bin/PSI/loginForms.asmx";
            loginForms.CookieContainer = cookies;
            queue.Url = URL + "_vti_bin/PSI/queuesystem.asmx";

            if (!loginForms.Login(user, password))
            {
                throw new Exception("Could not log into Project Server at " + URL);
            }
            queue.CookieContainer = cookies;
        }

        public int NumMessages(PSLib.QueueConstants.QueueMsgType queueType, PSLib.QueueConstants.JobState jobState)
        {
                dv.RowFilter = "JobCompletionState = " + (int)jobState + " AND MessageType = " + (int)queueType;
                return dv.Count;
        }

        public int NumProcessing
        {
            get
            {
                dv.RowFilter = "JobCompletionState = " + (int)wsQueueSystem.JobState.Processing;
                return dv.Count;
            }
        }

        public int NumReadyForProcessing
        {
            get
            {
                dv.RowFilter = "JobCompletionState = " + (int)wsQueueSystem.JobState.ReadyForProcessing;
                return dv.Count;
            }
        }

        public int NumSuccess
        {
            get
            {
                dv.RowFilter = "JobCompletionState = " + (int)wsQueueSystem.JobState.Success;
                return dv.Count;
            }
        }

        public int NumCanceled
        {
            get
            {
                dv.RowFilter = "JobCompletionState = " + (int)wsQueueSystem.JobState.Canceled;
                return dv.Count;
            }
        }

        public int NumFailed
        {
            get
            {
                dv.RowFilter = "JobCompletionState = " + (int)wsQueueSystem.JobState.Failed;
                return dv.Count;
            }
        }

        public int NumCorrelationBlocked
        {
            get
            {
                dv.RowFilter = "JobCompletionState = " + (int)wsQueueSystem.JobState.CorrelationBlocked;
                return dv.Count;
            }
        }

        public DataTable GetJobs()
        {
            dsQueue = queue.ReadAllJobStatusSimple(DateTime.Now.AddDays(-7), DateTime.Now, 10000, true, wsQueueSystem.SortColumn.QueueEntryTime, wsQueueSystem.SortOrder.Ascending);
            dv.Table = dsQueue.Status;
            return dsQueue.Status;
        }

        public int GetJobCount()
        {
            return queue.GetJobCount(null, null, null);
        }
}
}


